package controller.backOffice;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Transient;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.primefaces.model.UploadedFile;

import ejb.author.AuthorService;
import ejb.book.BookService;
import entities.Author;
import entities.Book;
import entities.Category;
import entities.OrderItem;

@Named
@RequestScoped
public class BookController implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	private Book currentBook;

	private Book tempBook;

	private StreamedContent photo;

	@EJB
	private BookService bookService;

	private List<Book> books; 

	public List<Book> getBooks() {
		return bookService.findAll();
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	private List<Author> tempAuthor;

	//	public void setTempAuthor(Author tempAuthor) {
	//		currentBook.addAuthor(tempAuthor);		
	//		System.out.println("lol2");
	//	}

	public List<Author> getTempAuthor() {
		return tempAuthor;
	}

	public void setTempAuthor(List<Author> tempAuthor) {
		this.tempAuthor = tempAuthor;
	}

	private UploadedFile file;      

	private UploadedFile file2;   
	
	public UploadedFile getFile() {  
		return file;  
	}    

	public void setFile(UploadedFile file) {  
		this.file = file;  
	}
	

	public UploadedFile getFile2() {
		return file2;
	}

	public void setFile2(UploadedFile file2) {
		this.file2 = file2;
	}

	public String addBook() {
		Book b1 = new Book();
		b1.setTitle(currentBook.getTitle());
		b1.setPrice(currentBook.getPrice());
		b1.setCategory(currentBook.getCategory());
		b1.setAuthors(tempAuthor);
		b1.setPhoto(file.getContents());
		bookService.create(b1);
		return "listeBook";
	}

	public String addNewBook(){
		return "addNewBook";
	}


	//	public String add() {
	//		System.out.println("Book title " + currentBook.getTitle());
	//		//System.out.println("getFile().getSize() " + getFile().getSize());
	//		/*if (getFile() != null && getFile().getSize() > 0) {	
	//			currentBook.setPhoto(getFile().getContents());
	//		}*/
	//		currentBook.setAuthors(new ArrayList<Author>());
	//		currentBook.setCategory(new Category());
	//		currentBook.setDate(new Date());
	//		currentBook.setPrice(new BigDecimal(13));
	//		currentBook.setOrderItems(new ArrayList<OrderItem>());
	//
	//		bookService.create(currentBook);
	//		return "welcome";
	//	}

	public String editer(){
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String bookId1 = (String)requestMap.get("bookId");  
		Long bookId = Long.parseLong(bookId1);		
		currentBook=bookService.find(bookId);
		return "editBook";
	}

	public String edit1(){
		Book b = new Book();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String bookId1 = (String)requestMap.get("bookId");  
		Long bookId = Long.parseLong(bookId1);		
		b=bookService.find(bookId);
		if (null != currentBook.getTitle()){
			b.setTitle(currentBook.getTitle());			
		}
		if (null != currentBook.getCategory()){
			b.setCategory(currentBook.getCategory());			
		}
		if (null != currentBook.getPrice()){			
			b.setPrice(currentBook.getPrice());
		}

		//b.setPhoto(file2.getContents());

		if (null != currentBook.getAuthors()){
			b.setAuthors(tempAuthor);			
		}
		bookService.update(b);
		return "listeBook";
	}

	public String delete() { 
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String bookId1 = (String)requestMap.get("bookId");  
		Long bookId = Long.parseLong(bookId1);
		if (bookId == null ){
			return "welcome";	
		}
		else if (bookId != null){
			bookService.delete(bookId);	
		}
		return "listeBook";
	}

	public StreamedContent getPhoto() throws IOException {
		FacesContext context = FacesContext.getCurrentInstance();

		if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
			// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
			return new DefaultStreamedContent();
		} else {
			Map requestMap = context.getExternalContext().getRequestParameterMap(); 
			String bookId1 = (String)requestMap.get("bookId");  
			Long bookId = Long.parseLong(bookId1);		
			Book b=bookService.find(bookId); 
			return new DefaultStreamedContent(new ByteArrayInputStream(b.getPhoto()));
		}		
	}

	public String gestionBook(){
		return "listeBook";
	}

	public Book getCurrentBook() {
		return currentBook;
	}

	public void setCurrentBook(Book currentBook) {
		this.currentBook = currentBook;
	}



	public Book getTempBook() {
		return tempBook;
	}

	public void setTempBook(Book tempBook) {
		this.tempBook = tempBook;
	}	

}
